package com.uenpay.oem.ui.user.info;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.cache.HttpStreamCache;
import com.qiyunxin.android.http.listener.OnHttpRequestTextListener;
import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qiyunxin.android.http.net.HttpRequestAsyncTaskQueue;
import com.qiyunxin.android.http.net.HttpRequestType;
import com.qiyunxin.android.http.net.HttpTextAsyncTask;
import com.qyx.android.message.type.PacketType;
import com.qyx.android.weight.json.JSONObject;
import com.socks.library.KLog;
import com.uenpay.im.IMApplication;
import com.uenpay.im.callback.OnCommonCallBack;
import com.uenpay.im.callback.OnUploadFile2Listener;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.invokeitems.UploadFile;
import com.uenpay.im.invokeitems.person.GetPersonInfoInvokeItem;
import com.uenpay.im.invokeitems.person.UpdateInfoInvokItem;
import com.uenpay.im.ui.old.activity.personal.UpdatePersonInfoCommonHandle;
import com.uenpay.oem.MyApplication;
import com.uenpay.oem.data.bean.UserAuthInfo;
import com.uenpay.oem.data.model.IUserAuthModel;
import com.uenpay.oem.data.modelImpl.UserAuthModel;
import com.uenpay.util.net.listener.HttpOnNextListener;

import org.json.JSONException;

/**
 * Author: liulou
 * Date: 2017/6/19.
 * desc:
 */

public class UserInfoPresenter implements UserInfoContract.Presenter {

    private UserInfoContract.View mUserInfoView;
    private IUserAuthModel mAuthModel;

    public UserInfoPresenter(UserInfoContract.View view) {
        mUserInfoView = view;
        mAuthModel = new UserAuthModel();
    }

    @Override
    public void getPersonInfo() {
        if (mUserInfoView != null) mUserInfoView.showLoading();
        mAuthModel.userInfoQuery(new HttpOnNextListener<QYXUserEntity>() {
            @Override
            public void onNext(QYXUserEntity qyxUserEntity) {
                if (mUserInfoView != null) {
                    mUserInfoView.showPersonInfo(qyxUserEntity);
                }

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                if (mUserInfoView != null) {
                    mUserInfoView.dismissLoading();
                    mUserInfoView.showToast(e.getMessage());
                }
            }
        });
    }

    @Override
    public void getAuthInfo() {
        mAuthModel.userCreditVerify(new HttpOnNextListener<UserAuthInfo>() {
            @Override
            public void onNext(UserAuthInfo userAuthInfo) {
                KLog.d(userAuthInfo.toString());
                MyApplication.setUserAuthInfo(userAuthInfo);
                mUserInfoView.showUserAuthInfo(userAuthInfo);
            }
        });
    }

    @Override
    public void uploadAvatar(byte[] bytes, String url) {
        new UploadFile(bytes, new OnUploadFile2Listener() {

            @Override
            public void OnUploadFileSuccess(String response) {
                JSONObject json;
                try {
                    json = new JSONObject(response);
                    try {
                        int status = json.optInt("status");
                        String msg = json.optString("msg");
                        IMApplication.showToast(msg);
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            // TODO
                            String small = APIConfiguration.getAvatarUrlNormal(
                                    IMApplication.getCustId(),
                                    PacketType.PERSONAL);
                            HttpStreamCache.getInstance().ClearCacheBitmap(
                                    small);
                            HttpStreamCache.getInstance().ClearCacheBitmap(
                                    APIConfiguration.getAvatarUrlBig(
                                            IMApplication.getCustId(),
                                            PacketType.PERSONAL));
                            if (mUserInfoView != null) mUserInfoView.changeAvatar(small);
                        }

                    } catch (Exception e) {
                    }
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
                String patern = APIConfiguration.getBaseUrl()
                        + "Cust/Cust/getCustQRCode?"
                        + APIConfiguration.getCustIdAndToken();
                HttpStreamCache.getInstance().ClearCacheBitmap(patern);
                if (mUserInfoView != null) {
                    mUserInfoView.showToast("头像更换成功");
                    mUserInfoView.dismissLoading();
                }
            }

            @Override
            public void OnUploadFileFail(String response) {
                if (mUserInfoView != null) {
                    mUserInfoView.showToast("上传失败");
                    mUserInfoView.dismissLoading();
                }
            }

        }, url);
    }

    @Override
    public void updateInfo(String field, String value) {
        UpdatePersonInfoCommonHandle.udpatePersonInfo(field, value,
                new OnCommonCallBack() {

                    @Override
                    public void onSuccess(Object result) {
                        UpdateInfoInvokItem.UpdateInfoInvokItemResult results = (UpdateInfoInvokItem.UpdateInfoInvokItemResult) result;
                        if (results != null
                                && results.status == HttpInvokeResultCode.RESULT_OK) {
                            IMApplication.showToast(results.msg);
                            getPersonInfo();
                        }
                    }

                    @Override
                    public void onFailed(String reason) {
                        IMApplication.showToast(reason);
                    }
                });
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDestroy() {
        if (mUserInfoView != null) {
            mUserInfoView = null;
        }
    }
}
